package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.Building;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.BuildDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.service.BuildService;
import com.github.pagehelper.PageInfo;

/**
 * 楼号管理  2018年1月4日20:40:09
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/build")
public class BuildController{

	@Resource
	private BuildService buildService;
	/**
	 * 根据筛选条件获取楼号列表   2018年1月4日20:40:32
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getBuildList(@RequestBody Map<String,String> map){
		PageInfo<BuildDto> bulidList = buildService.getBulidList(map);
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> pageMap = new HashMap<>();
		resultMap.put("list", bulidList.getList());
		pageMap.put("total", bulidList.getTotal());
		resultMap.put("pageInfo", pageMap);
		
		return SysResult.ok(0, "success",resultMap);
	}
	/**
	 * 根据筛选条件导出科室信息 Excel  2018年1月9日18:05:32
	 * @param map
	 * @return
	 */
	@RequestMapping("excel")
	public void getBuildListExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			String buildName = request.getParameter("buildingName");
			if(!StringUtils.isEmpty(buildName)){
				buildName = buildName.trim();
				map.put("buildingName", buildName);
			}
			List<BuildDto> bulidListExcel = buildService.bulidListExcel(map);
			String finalFileName = URLEncoder.encode("楼号管理_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, bulidListExcel, BuildDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 根据Id获取楼号信息  2018年1月5日09:48:58
	 * @param map
	 * @return
	 */
	@RequestMapping("query")
	public SysResult getBuild(@RequestBody Map<String,String> map){
		BuildDto bulidById = buildService.getBulidById(map);
		return SysResult.ok(0, "success",bulidById);
	}
	/**
	 * 楼号下拉列表  2018年1月5日16:32:26
	 * @return
	 */
	@RequestMapping("build-pull")
	public SysResult getPull(){
		List<PullDownDto> bulidPull = buildService.getBulidPull();
		return SysResult.ok(0, "success",bulidPull);
	}
	/**
	 * 楼层下拉列表  2018年1月5日16:32:26
	 * @return
	 */
	@RequestMapping("floor-pull")
	public SysResult getFloor(){
		List<PullDownDto> floorPull = buildService.getFloorPull();
		return SysResult.ok(0, "success",floorPull);
	}
	/**
	 * 保存楼号信息   2018年1月5日10:05:51
	 * @param map
	 * @return
	 */
	@RequestMapping("insert")
	public SysResult insertBuild(@RequestBody Building build){
		int insertBulid = buildService.insertBulid(build);
		if(insertBulid <= 0){
			if(insertBulid == -1){
				return SysResult.error(2, "该楼号已存在，请勿重复提交");
				
			}else{
				return SysResult.error(1, "保存信息失败");
				
			}
		}
		return SysResult.ok(0, "保存成功");
	}
	/**
	 * 更新楼号信息   2018年1月5日10:16:27
	 * @param build
	 * @return
	 */
	@RequestMapping("update")
	public SysResult updateBuild(@RequestBody Building build){
		if(StringUtils.isEmpty(build.getBuildingId())){
			return SysResult.error(3, "楼号Id为空");
		}
		int updateBuild = buildService.updateBuild(build);
		if(updateBuild <= 0){
			if(updateBuild == -1){
				return SysResult.error(2, "该楼号已存在，请勿重复提交");
				
			}else{
				return SysResult.error(1, "修改信息失败");
				
			}
		}
		return SysResult.ok(0, "修改成功");
	}
	/**
	 * 删除楼号信息  2018年1月5日10:16:42
	 * @param build
	 * @return
	 */
	@RequestMapping("delete")
	public SysResult deleteBuild(@RequestBody Map<String,String> map){
		String buildingId = map.get("buildingId");
		SysResult deleteBuild = buildService.deleteBuild(buildingId);
		
		return deleteBuild;
	}
}
